Skip to content

fix(define): fix match escaped dots to support $-prefixed define keys - #23249

Merged
bluwy merged 1 commit into
vitejs:mainfrom
contactjawad:fix-define-escaped-dot-regex
Aug 13, 2026
Merged

fix(define): fix match escaped dots to support $-prefixed define keys#23249
bluwy merged 1 commit into
vitejs:mainfrom
contactjawad:fix-define-escaped-dot-regex

Conversation

@contactjawad

Copy link
Copy Markdown
Contributor

Description

The define plugin silently drops define keys that contain a $ (e.g. define: { $FOO: '"bar"' }) — the transform leaves the source unchanged instead of replacing $FOO.

Root cause: escapedDotRE = /(?<!\\)\\./g is meant to rewrite an escaped dot (\.) inside an already-escapeRegex-escaped key into \??\. (so it also matches optional chaining, ?.). But the . in that regex is unescaped, so it actually matches a backslash followed by any character. escapeRegex('$FOO') produces \$FOO; the replaceAll then rewrites \$\??\., producing the pattern \??\.FOO, which never matches $FOO. The pattern.test(code) pre-check is then false and the replacement is skipped.

Fix: escape the dot (/(?<!\\)\\\./g) so only an escaped literal dot is rewritten. Dotted keys like import.meta.env are unaffected (both regexes yield foo\??\.bar), while escaped metacharacters such as \$ are left intact.

Added a regression test (replaces define keys containing $) that fails on main (the transform returns undefined) and passes with this change.

…keys are replaced

escapedDotRE was /(?<!\\)\\./g, whose `.` is unescaped, so it matched a
backslash followed by any character. For a define key like `$FOO`, escapeRegex
produces `\$FOO`, and the replaceAll then rewrote `\$` to `\??\.`, producing the
pattern `\??\.FOO`, which never matches `$FOO`. The `pattern.test(code)`
pre-check was therefore false and the define was silently skipped.

Escape the dot so only an escaped literal dot is rewritten. Dotted keys like
`import.meta.env` are unaffected (both regexes yield `foo\??\.bar`), while
escaped metacharacters such as `\$` are now left intact.
@bluwy

bluwy commented Aug 13, 2026

Copy link
Copy Markdown
Member

/ecosystem-ci run

@pkg-pr-new

pkg-pr-new Bot commented Aug 13, 2026

Copy link
Copy Markdown

Open in StackBlitz

@vitejs/plugin-legacy

pnpm add https://pkg.pr.new/@vitejs/plugin-legacy@23249 -D
npm i https://pkg.pr.new/@vitejs/plugin-legacy@23249 -D
yarn add https://pkg.pr.new/@vitejs/plugin-legacy@23249.tgz -D

vite

pnpm add https://pkg.pr.new/vite@23249 -D
npm i https://pkg.pr.new/vite@23249 -D
yarn add https://pkg.pr.new/vite@23249.tgz -D

commit: 4c9b5a0

@vite-ecosystem-ci

This comment has been minimized.

@vite-ecosystem-ci

Copy link
Copy Markdown

📝 Ran ecosystem CI on 9f5e003: Open

suite result latest scheduled
astro failure failure
qwik failure failure
react-router failure failure
tanstack-start ⏹️ cancelled ⏹️ cancelled
sveltekit failure failure
vite-plugin-rsc failure failure
vite-plugin-cloudflare success failure
vite-setup-catalogue success failure
vitest failure failure

laravel, marko, module-federation, nuxt, storybook, quasar, vike, unocss, vite-plugin-react, vite-environment-examples, vite-plugin-vue, vite-plugin-pwa, waku, vite-plugin-svelte, vuepress, vitepress, analogjs

@bluwy bluwy changed the title fix(define): only match escaped dots in escapedDotRE so $-prefixed define keys are replaced fix(define): fix match escaped dots to support $-prefixed define keys Aug 13, 2026
@bluwy
bluwy merged commit dcf88bd into vitejs:main Aug 13, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants